package com.hy.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.hy.admin.helper.response.NewsListResponse;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.NewsEntity;
import com.hy.admin.enums.EnumIsRecommend;
import com.hy.admin.helper.request.NewsRequest;
import com.hy.admin.helper.response.NewsResponse;
import com.hy.admin.service.NewsService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;

/**
 * Created by yangqq on 18/4/5.
 * 新闻
 */
@RestController
@RequestMapping("/api/news")
public class NewsController {

    protected final Logger logBase = LoggerFactory.getLogger(NewsController.class);
    @Autowired
    private NewsService newsService;

    /**
     * 新闻列表
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponseDTO list(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("news_list_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        NewsRequest newsRequest= gson.fromJson(apiRequestDTO.getRequestData(), NewsRequest.class);
        //获取数据
        ServicesResult<List<NewsListResponse>> newsResponse = newsService.list(newsRequest.getPageNum(), newsRequest.getPageSize());
        apiResponseDTO.initServicesResult(newsResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(newsResponse.getResult()));
        logBase.info("news_list_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 新闻详情
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ApiResponseDTO detail(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("news_detail_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        NewsRequest newsRequest= gson.fromJson(apiRequestDTO.getRequestData(), NewsRequest.class);
        //获取数据
        ServicesResult<NewsResponse> newsResponse = newsService.detail(newsRequest.getId());
        apiResponseDTO.initServicesResult(newsResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(newsResponse.getResult()));
        logBase.info("news_detail_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 添加新闻
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponseDTO add(ApiRequestDTO apiRequestDTO,@RequestParam(required = false ,value = "files") MultipartFile[] files) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("news_add_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        NewsEntity newsEntity= gson.fromJson(apiRequestDTO.getRequestData(), NewsEntity.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = null;
        try {
            servicesResult = newsService.add(newsEntity, files);
        } catch (Exception e) {
            e.printStackTrace();
            servicesResult = new ServicesResult<>();
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("news_add_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 修改新闻
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponseDTO update(@RequestBody final ApiRequestDTO apiRequestDTO) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("news_update_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        NewsEntity newsEntity= gson.fromJson(apiRequestDTO.getRequestData(), NewsEntity.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = null;
        try {
            servicesResult = newsService.update(newsEntity);
        } catch (Exception e) {
            e.printStackTrace();
            servicesResult = new ServicesResult<>();
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("news_update_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 删除新闻
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ApiResponseDTO delete(@RequestBody final ApiRequestDTO apiRequestDTO) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("news_delete_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        NewsRequest newsRequest= gson.fromJson(apiRequestDTO.getRequestData(), NewsRequest.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = null;
        try {
            servicesResult = newsService.delete(newsRequest.getIds());
        } catch (Exception e) {
            e.printStackTrace();
            servicesResult = new ServicesResult<>();
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("news_delete_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 设置新闻推荐
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/setRecommend", method = RequestMethod.POST)
    public ApiResponseDTO setRecommend(@RequestBody final ApiRequestDTO apiRequestDTO) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("news_setRecommend_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        NewsRequest newsRequest= gson.fromJson(apiRequestDTO.getRequestData(), NewsRequest.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = newsService.updateRecommend(newsRequest.getId(), EnumIsRecommend.YES.getId());
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("news_setRecommend_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 设置新闻推荐
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/cancelRecommend", method = RequestMethod.POST)
    public ApiResponseDTO cancelRecommend(@RequestBody final ApiRequestDTO apiRequestDTO) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("news_cancelRecommend_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        NewsRequest newsRequest= gson.fromJson(apiRequestDTO.getRequestData(), NewsRequest.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = newsService.updateRecommend(newsRequest.getId(), EnumIsRecommend.NO.getId());
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("news_cancelRecommend_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
    
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public ServicesResult<Object> searchNews(String keyWord, Integer pageNum, Integer pageSize) {
    	ServicesResult<Object> result = new ServicesResult<Object>();
    	PageInfo<NewsEntity> pageInfo = newsService.listByKeyWord(keyWord, pageNum, pageSize);
    	result.setResult(pageInfo);
        return result;
    }
}
